// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // 主角跳跃高度
        jumpHeight: 0,
        // 主角跳跃持续时间
        jumpDuration: 0,
        // 最大移动速度
        maxMoveSpeed: 0,
        // 加速度
        accel: 0,
        //跳的音效
        jump_music : {
            default: null,
            type: cc.AudioClip
        },
    },

    onLoad () {
        this.jumpAction = this.setJumpAction();
        this.node.runAction(this.jumpAction);

        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;
        // 主角当前水平方向速度
        this.xSpeed = 0;

        // 初始化键盘输入监听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);   
    },

    onKeyDown (event) {
        // set a flag when key pressed
        switch(event.keyCode) {
            case 37: //<-
                this.accLeft = true;
                break;
            case 39: //->
                this.accRight = true;
                break;
            case 32: //原地跳动
                // 加速度方向开关
                this.accLeft = false;
                this.accRight = false;
                // 主角当前水平方向速度
                this.xSpeed = 0;
                break;
        }
    },

    onKeyUp (event) {
        cc.log('player.js onKeyUp');
        // unset a flag when key released
        switch(event.keyCode) {
            case 37: //<-
                this.accLeft = false;
                break;
            case 39: //->
                this.accRight = false;
                break;
        }
    },

    start () {
        cc.log('start');
    },

    update (dt) {
        // cc.log('update');
         // 根据当前加速度方向每帧更新速度
         if (this.accLeft) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;
        var windowSize =cc.winSize.width;
        if(this.node.x > windowSize/2){
            cc.log('右边超出屏幕，人物从左边显示');
            this.node.x = -windowSize/2;
        }
        if(this.node.x < -windowSize/2){
            cc.log('左边超出屏幕，人物从右边边显示');
            this.node.x = windowSize/2;
        }
    },

    setJumpAction: function () {
        var finished = cc.callFunc(function(target, jumpMusic) {
            cc.audioEngine.play(jumpMusic, false, 1);
        }, this, this.jump_music); 
        // 跳跃上升
        var jumpUp = cc.moveBy(this.jumpDuration, cc.v2(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 下落
        var jumpDown = cc.moveBy(this.jumpDuration, cc.v2(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
        // 不断重复
        return cc.repeatForever(cc.sequence(jumpUp, jumpDown, finished));
    },

    onDestroy () {
        // 取消键盘输入监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    //开始游戏
    onclick_startPlayBtn: function () {
        cc.director.loadScene("game_playing");
    },

    //结束游戏
    game_over () {
        cc.director.loadScene("game_over");
    },
});
